package testHelper;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;



public class Scraper
{
	private String fileLocation;
	private final int nine = 9;
	int size =0;
	ArrayList<ArrayList<String>> table;

	public Scraper(String fileLocation)
	{
		this.fileLocation=fileLocation;
		table = new ArrayList<ArrayList<String>>(); 
		init();
	}

	private void init()
	{
		try
		{
			File reportFile = new File(fileLocation);
			Document doc = Jsoup.parse(reportFile, "UTF-8");
			Element table = doc.select("table").first();
			if (table==null)
				return;
			//System.out.println(table.toString());
			Elements rows = table.select("tr");
			Iterator<Element> itr=rows.iterator();

			//get first list which is headers
			Element ele = itr.next();
			while(itr.hasNext())
			{
				ele = itr.next();
				Elements column = ele.select("td");
				ArrayList<String> tmp = new ArrayList<String>();
				for (int x=0; x<= nine ;x++)
				{
					tmp.add(x, column.get(x).text());
					//System.out.println("name"+ele.getElementsByIndexEquals(0).text());
				}
				this.table.add(tmp);
				//System.out.println(ele.text());
			}

		}
		catch (IOException e)
		{
			e.printStackTrace();
		}
	}

	public Iterator<String> getTableIterator()
	{
		return new ArrayList<String>().iterator();
	}

	public static void main(String[] vars)
	{
		System.out.println("started Program");
		Scraper p= new Scraper("reports/test3.html");
		Iterator<ArrayList<String>> itr = p.getLineIterator();
		while(itr.hasNext())
		{
			ArrayList row =itr.next();
			System.out.println( "name " +row.get(0) +" last coulmn "+ row.get(row.size()-1));
		}
	}

	public Iterator<ArrayList<String>> getLineIterator()
	{
		return table.iterator();
	}
	
	public ArrayList<String> getColumn(int x)
	{
		return table.get(x);
	}


}
